package com.xmu.prcs.action;

import java.util.ArrayList;
import java.util.List;

import com.xmu.prcs.action.bean.PostBean;
import com.xmu.prcs.action.bean.TopicBean;
import com.xmu.prcs.action.bean.UserInfoBean;
import com.xmu.prcs.action.bean.UserReplyBean;
import com.xmu.prcs.service.ForumManager;
import com.xmu.prcs.service.UserManager;
import com.xmu.prcs.util.PageBean;
import com.xmu.prcs.util.ResultBean;


/**
 * 论坛
 * @author frequent
 * @version 1.0,2012-3-20
 *
 */
@SuppressWarnings("serial")
public class ForumAction extends BaseAction{

	private ForumManager forumManager;
	
	private UserManager userManager;
	
	private List<TopicBean> topicList;
	
	private ResultBean postPage;
	
	/**
	 * 界面上显示的页码
	 */
	private List<String> pageNumber;
	
	/**
	 * 与界面交互的分页封装类
	 */
	private PageBean page = new PageBean();
	
	/**
	 * 分页时的最后一页
	 */
	private String endPage;
	
	/**
	 * 翻页
	 * -1表示向前翻页
	 * 0表示不翻页
	 * 1表示向后翻页
	 */
	private int move = 0;
	
	/**
	 * 判断是增加还是修改
	 */
	private int addOrEdit;
	
	private TopicBean topicBean;
	
	private int topicId;
	
	private int idPost;
	
	private List<UserInfoBean> userList;
	
	private List<String> chargePerson;
	
	private List<PostBean> postList;
	
	private int postId;
	
	private PostBean post;
	
	/**
	 * 某个帖子已有的版主
	 */
	private List<UserInfoBean> chargeUser;
	
	private ResultBean userReplyPage;
	
	private List<UserReplyBean> userReplys;
	
	private UserReplyBean userReply;
	
	/**
	 * 回复Id
	 */
	private int replyTo;
	
	/**
	 * 判断是否是版主
	 */
	private int isChargePerson;
	
	private int replyId;

	public ForumManager getForumManager() {
		return forumManager;
	}

	public void setForumManager(ForumManager forumManager) {
		this.forumManager = forumManager;
	}
	
	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public List<TopicBean> getTopics() {
		return topicList;
	}

	public void setTopics(List<TopicBean> topics) {
		this.topicList = topics;
	}

	public List<String> getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(List<String> pageNumber) {
		this.pageNumber = pageNumber;
	}

	public PageBean getPage() {
		return page;
	}

	public void setPage(PageBean page) {
		this.page = page;
	}

	public String getEndPage() {
		return endPage;
	}

	public void setEndPage(String endPage) {
		this.endPage = endPage;
	}

	public int getMove() {
		return move;
	}

	public void setMove(int move) {
		this.move = move;
	}

	public List<TopicBean> getTopicList() {
		return topicList;
	}

	public void setTopicList(List<TopicBean> topicList) {
		this.topicList = topicList;
	}

	public int getAddOrEdit() {
		return addOrEdit;
	}

	public void setAddOrEdit(int addOrEdit) {
		this.addOrEdit = addOrEdit;
	}

	public TopicBean getTopicBean() {
		return topicBean;
	}

	public void setTopicBean(TopicBean topicBean) {
		this.topicBean = topicBean;
	}

	public int getTopicId() {
		return topicId;
	}

	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}

	public int getIdPost() {
		return idPost;
	}

	public void setIdPost(int idPost) {
		this.idPost = idPost;
	}

	public List<UserInfoBean> getUserList() {
		return userList;
	}

	public void setUserList(List<UserInfoBean> userList) {
		this.userList = userList;
	}

	public List<String> getChargePerson() {
		return chargePerson;
	}

	public void setChargePerson(List<String> chargePerson) {
		this.chargePerson = chargePerson;
	}

	public List<UserInfoBean> getChargeUser() {
		return chargeUser;
	}

	public void setChargeUser(List<UserInfoBean> chargeUser) {
		this.chargeUser = chargeUser;
	}

	public ResultBean getPostPage() {
		return postPage;
	}

	public void setPostPage(ResultBean postPage) {
		this.postPage = postPage;
	}

	public List<PostBean> getPostList() {
		return postList;
	}

	public void setPostList(List<PostBean> postList) {
		this.postList = postList;
	}

	public int getPostId() {
		return postId;
	}

	public void setPostId(int postId) {
		this.postId = postId;
	}

	public PostBean getPost() {
		return post;
	}

	public void setPost(PostBean post) {
		this.post = post;
	}

	public ResultBean getUserReplyPage() {
		return userReplyPage;
	}

	public void setUserReplyPage(ResultBean userReplyPage) {
		this.userReplyPage = userReplyPage;
	}

	public List<UserReplyBean> getUserReplys() {
		return userReplys;
	}

	public void setUserReplys(List<UserReplyBean> userReplys) {
		this.userReplys = userReplys;
	}

	public UserReplyBean getUserReply() {
		return userReply;
	}

	public void setUserReply(UserReplyBean userReply) {
		this.userReply = userReply;
	}

	public int getReplyTo() {
		return replyTo;
	}

	public void setReplyTo(int replyTo) {
		this.replyTo = replyTo;
	}

	public int getIsChargePerson() {
		return isChargePerson;
	}

	public void setIsChargePerson(int isChargePerson) {
		this.isChargePerson = isChargePerson;
	}

	public int getReplyId() {
		return replyId;
	}

	public void setReplyId(int replyId) {
		this.replyId = replyId;
	}

	/**
	 * 跳转到论坛首页
	 * @return
	 */
	public String toForumIndex(){
		
		topicList = forumManager.getAllTopic();
    	
		return SUCCESS;
	}
	
	/**
	 * 跳转到增加话题的页面
	 * @return
	 */
	public String toAddTopic(){
		userList = userManager.getAllUsers();
		addOrEdit = 1;
		return SUCCESS;
	}
	
	/**
	 * 增加话题
	 * @return
	 */
	public String addTopic(){
		forumManager.addTopic(topicBean, chargePerson);
		return SUCCESS;
	}
	
	/**
	 * 跳转到修改话题页面
	 * @return
	 */
	public String toModifyTopic(){
		topicBean = forumManager.getTopicById(topicId);
		userList = userManager.getAllUsers();
		chargeUser = forumManager.getChargePersonOfTopic(topicId);
		addOrEdit = 2;
		return SUCCESS;
	}
	
	/**
	 * 修改话题
	 * @return
	 */
	public String modifyTopic(){
		forumManager.modifyTopic(topicBean, chargePerson);
		return SUCCESS;
	}
	
	/**
	 * 删除话题
	 * @return
	 */
	public String deleteTopic(){
		forumManager.deleteTopic(topicId);
		return SUCCESS;
	}
	
	/**
	 * 帖子列表
	 * @return
	 */
	public String showPostList(){
		if(move == 1){
    		if(Integer.parseInt(endPage) == 0 ){
    			page.setCurrentPage(page.getCurrentPage() + 9);
    		}
    	}else if(move == -1){
    		if(page.getCurrentPage() > 9){
    			page.setCurrentPage(page.getCurrentPage() - 9);
    		}
    	}
		Integer userId = (Integer)this.getSession().getAttribute("userId");
		if(userId == null){
			userId = 0;
		}
		isChargePerson = forumManager.isChargePerson(topicId, userId);
		postPage = forumManager.getPost4Page(page, topicId, userId);
		postList = (List<PostBean>)postPage.getContent();
		//处理界面显示页码，从当前页码开始
    	pageNumber = new ArrayList<String>();
    	if(postPage.getPage().getCountPage() - postPage.getPage().getCurrentPage() + 1 <= 10){//最多显示10个页码
    		for(int i = postPage.getPage().getCurrentPage() ; i <= postPage.getPage().getCountPage(); i++){
    			System.out.println(i);
    			pageNumber.add(Integer.toString(i));
    		}
    	}else{
    		for(int i = postPage.getPage().getCurrentPage(); i <= postPage.getPage().getCurrentPage() + 9; i++){
    			pageNumber.add(Integer.toString(i));
    		}
    		pageNumber.add("0");//表示省略号
    	}
		return SUCCESS;
	}
	
	/**
	 * 跳转到增加帖子页面
	 * @return
	 */
	public String toAddPost(){
		addOrEdit = 1;
		return SUCCESS;
	}
	
	/**
	 * 增加帖子
	 * @return
	 */
	public String addPost(){
		Integer userId = (Integer)this.getSession().getAttribute("userId");
		forumManager.addPost(post, topicId, userId);
		return SUCCESS;
	}
	
	/**
	 * 跳转到修改帖子页面
	 * @return
	 */
	public String toModifyPost(){
		post = forumManager.getPostById(postId);
		addOrEdit = 2;
		return SUCCESS;
	}
	
	/**
	 * 修改帖子
	 * @return
	 */
	public String modifyPost(){
		forumManager.modifyPost(post);
		return SUCCESS;
	}
	
	/**
	 * 删除帖子
	 * @return
	 */
	public String deletePost(){
		forumManager.deletePost(postId, topicId);
		return SUCCESS;
	}
	
	/**
	 * 回复一个帖子
	 * @return
	 */
	public String addReply(){
//		try {
//			userReply.setContent(new String(userReply.getContent().getBytes("ISO8859-1"),"UTF-8"));
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
//			return INPUT;
//		}
		Integer userId = (Integer)this.getSession().getAttribute("userId");
		forumManager.addReply2Post(postId, userReply, userId, replyTo);
		page = new PageBean(forumManager.countReply(postId));
		return SUCCESS;
	}
	
	/**
	 * 帖子的回复列表
	 * @return
	 */
	public String showReply(){
		if(move == 1){
    		if(Integer.parseInt(endPage) == 0 ){
    			page.setCurrentPage(page.getCurrentPage() + 9);
    		}
    	}else if(move == -1){
    		if(page.getCurrentPage() > 9){
    			page.setCurrentPage(page.getCurrentPage() - 9);
    		}
    	}
		Integer userId = (Integer)this.getSession().getAttribute("userId");
		post = forumManager.getPostById(postId);
		if(userId == null){
			userId = 0;
		}
		userReplyPage = forumManager.showReplysOfPost(postId, page, userId);
		userReplys = (List<UserReplyBean>)userReplyPage.getContent();
		//处理界面显示页码，从当前页码开始
    	pageNumber = new ArrayList<String>();
    	if(userReplyPage.getPage().getCountPage() - userReplyPage.getPage().getCurrentPage() + 1 <= 10){//最多显示10个页码
    		for(int i = userReplyPage.getPage().getCurrentPage() ; i <= userReplyPage.getPage().getCountPage(); i++){
    			System.out.println(i);
    			pageNumber.add(Integer.toString(i));
    		}
    	}else{
    		for(int i = userReplyPage.getPage().getCurrentPage(); i <= userReplyPage.getPage().getCurrentPage() + 9; i++){
    			pageNumber.add(Integer.toString(i));
    		}
    		pageNumber.add("0");//表示省略号
    	}
		return SUCCESS;
	}
	
	/**
	 * 删除一个回复
	 * @return
	 */
	public String deleteReply(){
		forumManager.deleteReply(postId, replyId);
		return SUCCESS;
	}
}
